module SOD
  
  class FileManager
    
    def initialize
      @hash = Hash.new
    end
    
    def update
      Plugin.model.projectInfo.getConstruction
      
      @hash['roomsize'] = Plugin.model.projectInfo.roomsize
      @hash['flexibility'] = Plugin.model.projectInfo.flexibility
      @hash['numberOfStoreys'] = Plugin.model.projectInfo.numberOfStoreys
      @hash['origin'] = Plugin.model.sketchInfoContainer.pointOfOrigin
      @hash['direction'] = Plugin.model.sketchInfoContainer.direction.to_s
      @hash['lenght'] = Plugin.model.sketchInfoContainer.footprintLength
      @hash['width'] = Plugin.model.sketchInfoContainer.footprintWidth
      @hash['fpPoints'] = Plugin.model.sketchInfoContainer.footprintPoints
    end
    
    def writeDataToFile(file)
      self.update
      @hash.keys.each { |key| file.puts(key + ": " + @hash[key].to_s) }
    end
    
    def readFile(file)
      line_array = file.readlines
      populateHash(line_array)
      updateRuby
    end
    
    def populateHash(line_array)
      line_array.each do |line|
        key, value = line.split(": ")
        @hash[key] = value
        puts(key + ": " + @hash[key].to_s) if $debug
      end
    end
    
    def updateRuby
      Plugin.model.projectInfo.roomsize = @hash['roomsize'].strip!
      Plugin.model.projectInfo.flexibility = @hash['flexibility'].strip!
      Plugin.model.projectInfo.numberOfStoreys = @hash['numberOfStoreys'].to_i
      puts pt_array = @hash['fpPoints'].split(")(")
      allpts = []
      pt_array.each do |pts|
        pts.delete!("()m")
        pt = pts.split(", ")
        pt.each do |p|
          p.strip!
          p.gsub!(",", ".")
        end
        allpts << pt
        puts allpts
      end
      allpts.map! { |arr| arr.map! { |v| v.to_f.m } }
      allpts.map! { |arr| arr = Geom::Point3d.new arr }
      Plugin.model.sketchInfoContainer.footprintPoints = allpts
      Plugin.model.sketchInfoContainer.update
    end
    
  end
  
end
